// pages/login/index.js
import {
    wechat,
    manager,
    common,
    tour
} from "../../api/apiData"
import Toast from '@vant/weapp/toast/toast';
let util = require('../../utils/util.js')


Page({

    /**
     * 页面的初始数据
     */
    data: {
        code: "",
        username: "",
        password: "",
        captcha_code: '',
        captcha_img: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getComm()
    },
    getComm() {
        common.captcha({}, res => {
            this.setData({
                captcha_id: res.data.uniqid,
                captcha_img: res.data.content
            })
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    setusername(e) {
        var val = e.detail.value
        this.setData({
            username: val
        })
    },
    setpassword(e) {
        var val = e.detail.value
        this.setData({
            password: val
        })
    },
    setCode(e) {
        var val = e.detail.value
        this.setData({
            captcha_code: val
        })
    },
    Login() {
        let {
            username,
            password,
            captcha_id,
            captcha_code
        } = this.data
        tour.merchant.login({
            account: username,
            password: util.AesEncode(password),
            captcha_id: captcha_id,
            captcha_code: util.AesEncode(captcha_code)
        }, res => {
            let {
                code,
                msg,
                data
            } = res
            if (code == 1) {
                console.log(res)
                wx.setStorage({
                    key: "token2",
                    data: data.token
                })
                wx.setStorage({
                    key: "type",
                    data: 'Manage'
                })
                wx.showToast({
                    title: msg,
                })
                wx.navigateTo({
                    url: '/pages/manage/index',
                })
            } else {
                wx.showToast({
                    title: msg,
                    icon: 'error'
                })
                setTimeout(()=>{
                    this.getComm()

                },500)
            }

        })
    },
    VisiLogin() {
        let _this = this

        let iv, encryptedData

        wx.getUserProfile({
            desc: "个人信息",
            success: function (res) {

                iv = res.iv
                encryptedData = res.encryptedData
                Toast.loading()
                wx.login({
                    success(res) {

                        wechat.userLogin({
                            code: res.code,
                            iv,
                            encryptedData
                        }, res => {

                            let {
                                code,
                                msg,
                                data
                            } = res
                            console.log(res)
                            Toast.clear()
                            if (code == 0) {
                                Toast(msg)
                            } else {
                                wx.setStorage({
                                    key: "token",
                                    data: data.token
                                })
                                wx.setStorage({
                                    key: "type",
                                    data: 'User'
                                })
                                // wx.switchTab({
                                //   url: '/pages/index/index',
                                // })
                                wx.navigateBack({
                                    delta: 0,
                                })
                            }

                        })
                    }
                })

            },
            fail(err) {


            }
        })


    },
    toResign() {
        wx.navigateTo({
            url: '/pages/resign/index',
        })
    },
    WxLogin() {
        wx.login({
            success(res) {
                let {
                    code
                } = res
                tour.merchant.loginWxapp({
                    code
                }, res => {
                    let {
                        msg,
                        data
                    } = res
                    if (res.code == 1) {
                        console.log(res)
                        wx.setStorage({
                            key: "token2",
                            data: data.token
                        })
                        wx.setStorage({
                            key: "type",
                            data: 'Manage'
                        })
                        wx.showToast({
                            title: msg,
                        })
                        setTimeout(() => {
                            wx.navigateTo({
                                url: '/pages/manage/index',
                            })
                        }, 1000)
                    } else {
                        wx.showToast({
                            title: msg,
                            icon: 'error'
                        })
                    }
                })

            }
        })

    }
})